<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "edu_enter_order".
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property string $openid 用户openid
 * @property int $gift_id 礼包id
 * @property string $out_trade_no 订单号
 * @property string $total_fee 总费用
 * @property int $count 数量
 * @property int $status 0未支付 1已支付
 * @property int $create_time 订单生成时间
 * @property string $name 收件人姓名
 * @property string $phone 手机号
 * @property string $address 收货地址
 * @property string $logistics 物流
 * @property string $logistics_fee 物流费用
 * @property string $order_num 物流订单号
 * @property string $info 物流信息
 * @property string $remark 用户留言|用户备注
 * @property int $style 购买的等价 1是298 2是29800
 */
class EduEnterOrder extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'edu_enter_order';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'openid', 'gift_id', 'out_trade_no', 'total_fee', 'count', 'create_time', 'name', 'phone', 'address', 'style'], 'required'],
            [['user_id', 'gift_id', 'count', 'status', 'create_time', 'style'], 'integer'],
            [['total_fee', 'logistics_fee'], 'number'],
            [['openid', 'address'], 'string', 'max' => 60],
            [['out_trade_no', 'order_num'], 'string', 'max' => 50],
            [['name'], 'string', 'max' => 10],
            [['phone', 'logistics'], 'string', 'max' => 20],
            [['info', 'remark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'openid' => 'Openid',
            'gift_id' => 'Gift ID',
            'out_trade_no' => '订单号',
            'total_fee' => '实付金额(元)',
            'count' => '数量',
            'status' => '订单状态',
            'create_time' => '下单时间',
            'name' => '收件人姓名',
            'phone' => '收件人手机号',
            'address' => '收件人地址',
            'logistics' => '物流',
            'logistics_fee' => '物理费用',
            'order_num' => '物流订单号',
            'info' => '物流信息',
            'remark' => '备注',
            'style' => '礼包类型',
        ];
    }

    /**
     * @return array
     * 未支付的订单
     */
    public static function nPayment()
    {
        return (new \yii\db\Query())
            ->select(['COUNT(*) as count'])
            ->from('edu_enter_order')
            ->where(['status' => 0])
            ->all();
    }

    /**
     * @return array
     * 待发货
     */
    public static function stayDeliver()
    {
        return (new \yii\db\Query())
            ->select(['COUNT(*) as count'])
            ->from('edu_enter_order')
            ->where(['status' => 1])
            ->orWhere(['status' => 2])
            ->all();
    }

    /**
     * @return \yii\db\ActiveQuery
     * 跟edu_user表关联
     */
    public function getUser()
    {
        return $this->hasOne(EduUser::class, ['id'=> 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     * 跟礼包edu_enter_gifts关联
     */
    public function getGift()
    {
        return $this->hasOne(EduEnterGifts::class, ['id'=> 'gift_id']);
    }

    /**
     * @return array
     * 定义枚举
     */
    public static function attributeValue()
    {
        return [
            'status' => [
                '0' => '未支付',
                '1' => '已支付',
                '2' => '已发货',
                '3' => '已收货'
            ],
        ];
    }

    /**
     * @return string
     * 获取菜单
     */
    public function getStatusValue()
    {
        switch ($this->status)
        {
            case 0:
                $value = '未支付';
                break;
            case 1:
                $value = '已支付';
                break;
            case 2:
                $value = '已发货';
                break;
            case 3:
                $value = '已收货';
                break;
            default:
                $value = '';
                break;
        }
        return $value;
    }
}
